"""
Case Type   : bugfix
Case Name   : 内部函数array_integer_deleteidx\array_varchar_deleteidx可查询，不可调用
Create At   : 2025/6/12
Owner       : liu-tong-8848
Description :
    1、查询pg_proc是否包含array_integer_deleteidx\array_varchar_deleteidx
    2、\df查询array_integer_deleteidx\array_varchar_deleteidx
    3、调用array_integer_deleteidx\array_varchar_deleteidx
Expect      :
    1、包含
    2、成功
    3、合理报错
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:查询pg_proc是否包含array_integer_deleteidx或array_varchar_deleteidx；expect:成功----'
        self.log.info(text)
        sql_cmd = f"select * from pg_proc where oid in (7886, 7887);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('array_integer_deleteidx', sql_res, '执行失败' + text)
        self.assertIn('array_varchar_deleteidx', sql_res, '执行失败' + text)
        self.assertIn('(2 rows)', sql_res, '执行失败' + text)

        text = '----step2:\df查询array_integer_deleteidx或array_varchar_deleteidx；expect:成功----'
        self.log.info(text)
        sql_cmd = f"\df array_integer_deleteidx"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn('array_integer_deleteidx', sql_res, '执行失败' + text)
        self.assertIn('(1 row)', sql_res, '执行失败' + text)
        sql_cmd = f"\df array_varchar_deleteidx"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn('array_varchar_deleteidx', sql_res, '执行失败' + text)
        self.assertIn('(1 row)', sql_res, '执行失败' + text)

        text = '----step3:调用array_integer_deleteidx或array_varchar_deleteidx；expect:成功----'
        self.log.info(text)
        sql_cmd = f"select array_integer_deleteidx(array[1,2,3], 1);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn('ERROR', sql_res, '执行失败' + text)
        self.assertIn('array_integer_deleteidx must be call in procedure', sql_res, '执行失败' + text)
        sql_cmd = f"select array_varchar_deleteidx(array[1,2,3], 'a');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn('ERROR', sql_res, '执行失败' + text)
        self.assertIn('array_varchar_deleteidx must be call in procedure', sql_res, '执行失败' + text)

    def tearDown(self):
        self.log.info(f'----{os.path.basename(__file__)} end-----')
